<script setup>
import { ref } from "vue"
import { ElMessage } from 'element-plus'

// 表单数据
const formData = ref({
    name: "",
    password: "",
    authcode: ""
})
// 控制登录注册切换
const login_statu = ref(true)

// 注册
import { register, login } from '@/api/admin.js'
import { useTokenStore } from "@/stores/token";
import { useRouter } from "vue-router";
import useAdminStore from "@/stores/admin";
const Token = useTokenStore()
const adminer = useAdminStore()
const route = useRouter()

const doRegist = async () => {
    let result = await register(formData.value);
    result=result.data
    if (result.code == 1) {
        ElMessage.error('注册失败，'+result.msg)
    }
    else {
        ElMessage.success(result.msg ? result.msg : '注册成功，请登录')
        login_statu.value=true
    }
}


// 登录
const doLogin = async () => {
    delete formData.value['authcode']    
    let result = await login(formData.value)
    result=result.data
    if (result.code === 0) {
        ElMessage.success(result.msg)
        // 设置Token
        Token.setToken(result.data)
        // 设置用户信息
        adminer.setAdmin(formData.value)
        route.push("/")
    } else {
        ElMessage.error(result.msg)
    }
}
</script>

<template>
    <div class="main">
        <div class="container" v-if="login_statu">
            <div class="tit">登录</div>
            <input type="text" placeholder="账号" v-model="formData.name">
            <input type="password" placeholder="密码" v-model="formData.password">

            <button @click="doLogin">登录</button>
            <span>没有账号？<span href="" style="color:plum;text-decoration: none;cursor:pointer"
                    @click="login_statu = !login_statu">去注册</span></span>
        </div>
        <div class="container" v-else>
            <div class="tit">注册</div>
            <input type="text" placeholder="账号" v-model="formData.name">
            <input type="password" placeholder="密码" v-model="formData.password">
            <input type="password" placeholder="校验码" v-model="formData.authcode">
            <button @click="doRegist">注册</button>
            <span href="" style="color:plum;text-decoration: none;cursor:pointer" @click="login_statu = !login_statu">←
                返回登录 </span>
        </div>
        <div class="square">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
        <div class="circle">
            <ul>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>
</template>

<style scoped>
.main {
    /* 100%窗口高度 */
    height: 100vh;
    /* 弹性布局 居中 */
    display: flex;
    justify-content: center;
    align-items: center;
    /* 渐变背景 */
    background: linear-gradient(200deg, #e3c5eb, #a9c1ed);
    /* 溢出隐藏 */
    overflow: hidden;
}

.container {
    /* 相对定位 */
    position: relative;
    z-index: 1;
    background-color: #fff;
    border-radius: 15px;
    /* 弹性布局 垂直排列 */
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 350px;
    height: 500px;
    /* 阴影 */
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
}

.container .tit {
    font-size: 26px;
    margin: 65px auto 70px auto;
}

.container input {
    width: 280px;
    height: 30px;
    text-indent: 8px;
    border: none;
    border-bottom: 1px solid #ddd;
    outline: none;
    margin: 12px auto;
}

.container button {
    width: 280px;
    height: 40px;
    margin: 35px auto 40px auto;
    border: none;
    background: linear-gradient(-200deg, #fac0e7, #aac2ee);
    color: #fff;
    font-weight: bold;
    letter-spacing: 8px;
    border-radius: 10px;
    cursor: pointer;
    /* 动画过渡 */
    transition: 0.5s;
}

.container button:hover {
    background: linear-gradient(-200deg, #aac2ee, #fac0e7);
    background-position-x: -280px;
}

.container span {
    font-size: 14px;
}


ul li {
    position: absolute;
    border: 1px solid #fff;
    background-color: #fff;
    width: 30px;
    height: 30px;
    list-style: none;
    opacity: 0;
}

.square li {
    top: 40vh;
    left: 60vw;
    /* 执行动画：动画名 时长 线性的 无限次播放 */
    animation: square 10s linear infinite;
}

.square li:nth-child(2) {
    top: 80vh;
    left: 10vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
}

.square li:nth-child(3) {
    top: 80vh;
    left: 85vw;
    /* 设置动画延迟时间 */
    animation-delay: 4s;
}

.square li:nth-child(4) {
    top: 10vh;
    left: 70vw;
    /* 设置动画延迟时间 */
    animation-delay: 6s;
}

.square li:nth-child(5) {
    top: 10vh;
    left: 10vw;
    /* 设置动画延迟时间 */
    animation-delay: 8s;
}

.circle li {
    bottom: 0;
    left: 15vw;
    /* 执行动画 */
    animation: circle 10s linear infinite;
}

.circle li:nth-child(2) {
    left: 35vw;
    /* 设置动画延迟时间 */
    animation-delay: 2s;
}

.circle li:nth-child(3) {
    left: 55vw;
    /* 设置动画延迟时间 */
    animation-delay: 6s;
}

.circle li:nth-child(4) {
    left: 75vw;
    /* 设置动画延迟时间 */
    animation-delay: 4s;
}

.circle li:nth-child(5) {
    left: 90vw;
    /* 设置动画延迟时间 */
    animation-delay: 8s;
}

/* 定义动画 */
@keyframes square {
    0% {
        transform: scale(0) rotateY(0deg);
        opacity: 1;
    }

    100% {
        transform: scale(5) rotateY(1000deg);
        opacity: 0;
    }
}

@keyframes circle {
    0% {
        transform: scale(0) rotateY(0deg);
        opacity: 1;
        bottom: 0;
        border-radius: 0;
    }

    100% {
        transform: scale(5) rotateY(1000deg);
        opacity: 0;
        bottom: 90vh;
        border-radius: 50%;
    }
}
</style>